package com.billow.book.api;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.billow.book.model.Book;
import com.billow.book.model.vo.BookVo;
import com.billow.book.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookApi {

    @Autowired
    private BookService bookService;

    @PostMapping("/findPageBook/{pageSize}/{pageNo}")
    public Page<Book> findPageBook(@PathVariable(value = "pageSize") long pageSize,
                                   @PathVariable(value = "pageNo") long pageNo,
                                   @RequestBody BookVo bookVo) {
        return bookService.findPageBook(pageSize, pageNo, bookVo);
    }

    @GetMapping("/findBookById/{id}")
    public Book findBookById(@PathVariable(value = "id") Integer id) {
        return bookService.getById(id);
    }

    @PostMapping("/saveOrUpdate")
    public Book saveOrUpdate(@RequestBody BookVo bookVo) {
        bookVo.setUpdateTime(new Date());
        bookService.saveOrUpdate(bookVo);
        return BeanUtil.copyProperties(bookVo, Book.class);
    }

    @PostMapping("/bookDel/{id}")
    public boolean bookDel(@PathVariable(value = "id") Integer id) {
        return bookService.removeById(id);
    }
}
